import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.persistence.Player;
import com.persistence.TFootballTeam;
import com.persistence.TPhoto;
import com.persistence.TPlayer;
import com.service.PlayerService;
import com.service.TeamService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
@Transactional
public class TesServicePlayer {

	Log log = LogFactory.getLog(this.getClass());
	
	@Autowired
	PlayerService playerDao;
	
	@Autowired
	TeamService teamDao;
	
	@Test
	public void findAllPlayer(){
		List<TPlayer> list = playerDao.findAll(0);
		Assert.assertNotNull(list);
		System.out.println(list);
		for(TPlayer player : list) {
			log.info(player.getFirstname());
		}
	}
	
	@Test
	public void insertPlayer() {
		TFootballTeam team = teamDao.findById(1);

		TPhoto photo = new TPhoto();
		photo.setIdPhoto(playerDao.countAll()+1);
		photo.setData(new byte[]{'v'});
		
		Player player = new Player();
		player.setFirstname("Yahya");
		player.setLastname("Zainiss");
		player.setIdPlayer(playerDao.countAll()+1);
		player.setJoinDate(new Date());
		player.setSalary(500000d);
		player.setTFootballTeam(team);
		
		photo.setTPlayer(player);
		
		player.setTPhoto(photo);
		
		Assert.assertNotNull(playerDao.insert(player));
	}
	
	@Test
	public void findById(){
		TPlayer player = playerDao.findById(1);
		Assert.assertNotNull(player);
	}
	
//	@Test
//	public void updatePlayer() {
//		TPlayer player = playerDao.findById(playerDao.countAll());
//		
//		if(player instanceof Player) {
//			Player player2 = (Player) player; 
//			player2.setFirstname("Yahya");
//			player2.setLastname("Zainissdddd");
//			player2.setJoinDate(new Date());
//			player2.setSalary(5000d);
//			
//			Assert.assertNotNull(playerDao.update(player2));
//		}else{
//			Reserve reserve = (Reserve) player;
//			reserve.setFirstname("Yahya");
//			reserve.setLastname("Zainissdddd");
//			reserve.setJoinDate(new Date());
//			reserve.setCommision(50500d);
//			
//			Assert.assertNotNull(playerDao.update(reserve));
//		}
//		
//
//	}
//	
	@Test
	public void removePlayer() {
		TPlayer player = playerDao.findById(1);
		Assert.assertTrue(playerDao.remove(player));
	}
}
